﻿#region Usings

using System;
using System.Xml.XPath;
using Yandex.Market.Api.YmlSerialization;

#endregion

namespace Yandex.Market.Api.Model
{
    /// <summary>
    ///   Описание artist.title
    /// </summary>
    [YmlSerializeAs("offer")]
    [Serializable]
    public class OfferArtistTitle : OfferBase
    {
        /// <summary>
        /// 
        /// </summary>
        public OfferArtistTitle()
        {}

        internal OfferArtistTitle(IOffer offer) : base(offer)
        {}

        /// <summary>
        /// </summary>
        [YmlAttributeForClass]
        [YmlSerializeAs("type")]
        [YmlAttributeOrder(10)]
        public YmlOfferType OfferType
        {
            get { return YmlOfferType.ArtistTitle; }
        }

        /// <summary>
        ///   Исполнитель. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("artist")]
        [YmlElementOrder(200)]
        public string Artist { get; set; }

        /// <summary>
        ///   Название. Обязательный элемент.
        /// </summary>
        [YmlSerializeAs("title")]
        [YmlElementOrder(210)]
        public string Title { get; set; }

        /// <summary>
        ///   Год выпуска. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("year")]
        [YmlElementOrder(220)]
        public uint? Year { get; set; }

        /// <summary>
        ///   Носитель. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("media")]
        [YmlElementOrder(230)]
        public string Media { get; set; }

        /// <summary>
        ///   Актеры. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("starring")]
        [YmlElementOrder(240)]
        public string Starring { get; set; }

        /// <summary>
        ///   Режиссер. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("director")]
        [YmlElementOrder(250)]
        public string Director { get; set; }

        /// <summary>
        ///   Оригинальное название. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("originalName")]
        [YmlElementOrder(260)]
        public string OriginalName { get; set; }

        /// <summary>
        ///   Страна. Необязательный элемент.
        /// </summary>
        [YmlSerializeAs("country")]
        [YmlElementOrder(270)]
        public string Country { get; set; }

        internal static OfferArtistTitle CreateOfferArtistTitleFromXml(XPathNavigator nav, IOffer dummyOffer)
        {
            OfferArtistTitle offer = new OfferArtistTitle(dummyOffer);

            XPathNavigator node = nav.SelectSingleNode("artist");
            if(node != null)
                offer.Artist = node.Value;

            node = nav.SelectSingleNode("title");
            if(node != null)
                offer.Title = node.Value;

            uint uintVal;
            node = nav.SelectSingleNode("year");
            if(node != null && uint.TryParse(node.Value, out uintVal))
                offer.Year = uintVal;

            node = nav.SelectSingleNode("media");
            if(node != null)
                offer.Media = node.Value;

            node = nav.SelectSingleNode("starring");
            if(node != null)
                offer.Starring = node.Value;

            node = nav.SelectSingleNode("director");
            if(node != null)
                offer.Director = node.Value;

            node = nav.SelectSingleNode("originalName");
            if(node != null)
                offer.OriginalName = node.Value;

            node = nav.SelectSingleNode("country");
            if(node != null)
                offer.Country = node.Value;

            return offer;
        }
    }
}